package org.aeon.gamechatnest.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.aeon.gamechatnest.common.response.Result;
import org.aeon.gamechatnest.common.response.ResultBuild;
import org.aeon.gamechatnest.dto.UserLoginDto;
import org.aeon.gamechatnest.pojo.User;
import org.aeon.gamechatnest.service.UserService;
import org.aeon.gamechatnest.validated.Login;
import org.aeon.gamechatnest.validated.Register;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpHeaders;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author: MissingAeon
 * @date: 2024/5/9 13:48
 * @description:
 */
@Profile("sec")
@Slf4j
@Tag(name = "用户模块", description = "用户模块相关接口")
@RestController
@RequestMapping("/user")
@Validated
public class UserController {

    @Autowired
    private UserService userService;


    @Operation(summary = "用户登录", description = "用户登录接口")
    @PostMapping("/login")
    public Result login(@RequestBody @Validated(Login.class) UserLoginDto user) {
        // 处理登录逻辑
        log.debug("用户登录:{}", user);
        boolean login = userService.login(user);
        return ResultBuild.successIf(login).build();
    }

    @Operation(summary = "用户退出", description = "用户退出接口")
    @PostMapping("/logout")
    public Result logout(@RequestHeader(HttpHeaders.AUTHORIZATION) String token) {
        // 处理退出
        log.debug("用户退出:{}", token);
        userService.logout(token);
        return ResultBuild.result().successful().message("已退出登录").build();
    }


    @Operation(summary = "用户注册", description = "用户注册接口")
    @PostMapping("/register")
    public Result register(@RequestBody @Validated(Register.class) User user) {
        // 处理注册逻辑
        log.debug("用户注册:{}", user);
        boolean result = userService.register(user);
        return ResultBuild.successIf(result).message("注册成功").build();
    }
}
